package com.yyfax.ph.progress.search;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Title: ProgressPresenter Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年5月11日 下午7:08:57
 */
public class ProgressPresenter extends Presenter<ProgressModel, ProgressPresenter.View> {

	protected static final String GET_PROGRESS_FAILURE = "获取进度失败!";
	protected static final int REQUEST_CODE_PROGRESS_REFRESH = 19561;
	protected static final int REQUEST_CODE_PROGRESS_LOAD = 19562;

	public static class Generator implements PresenterFactory<ProgressPresenter> {
		@Override
		public ProgressPresenter create() {
			return new ProgressPresenter();
		}
	}

	public interface View extends IView {

		void refreshSuccess(List<ProgressModel.ProgressBean> list);

		void loadMoreSuccess(List<ProgressModel.ProgressBean> list);

		void onGetProgressFailure(String tips);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new ProgressModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getProgress(String section, String status, int pageNum, int pageSize) {

		ProgressModel.GetListParams params = new ProgressModel.GetListParams();
		params.section = section;
		params.status = status;
		params.pageNo = pageNum;
		params.pageSize = pageSize;
		if (pageNum == ProgressFragment.FIRST_PAGE) {
			execute(params, REQUEST_CODE_PROGRESS_REFRESH, false);
		} else {
			execute(params, REQUEST_CODE_PROGRESS_LOAD, false);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getProgressResult(ProgressModel.ProgressResult result) {
		if (isResponseSuccess(result)) {
			if (result.requestCode == REQUEST_CODE_PROGRESS_REFRESH) {
				mView.refreshSuccess(result.getContent());
			} else {
				mView.loadMoreSuccess(result.getContent());
			}
		} else {
			if (TextUtils.isEmpty(result.getMsg())) {
				mView.onGetProgressFailure(GET_PROGRESS_FAILURE);
			} else {
				mView.onGetProgressFailure(result.getMsg());
			}
		}
	}

}
